<?php
/**
 * prime/index.phtml
 * Vue par défaut pour les Primes
 */
?>
<?php
    $title = 'Primes';
    $this->headTitle($title); 
 
?>

<!-- ENTETE -->
<div class='content-head'>
    <h3>
	<?php echo $title ?>	
    </h3>
    <div class='toolbar'>
        <div class="toggle add" title="Ajouter">
            <a id='bt-add-prime' href="#" style="display: block; width: 100%; height: 100%;" ></a>
        </div>
    </div>
</div>

<!-- CONTENU -->
<div class='content-body'>
   <div class="add-toggle">
	<table class="table table-striped list-table">
	    <thead>
		<tr>
                    <td width='25%' style='text-align: center; font-weight: bold'>Libellé</td>
                    <td width='25%' style='text-align: center; font-weight: bold'>Valeur</td>
                    <td width='30%' style='text-align: center; font-weight: bold'>Description</td>
                    <td width='10%' style='text-align: center; font-weight: bold'>CP / Hors CP</td>
                    <td width='10%'>&nbsp;</td>
                </tr>
	    </thead>
	    <tbody>
		<tr>
		    <td>
			<a href="#" id="newPrimeLibelle" data-type="text"></a>
		    </td>
                    <td>
			<a href="#" id="newPrimeValeur" data-type="text"></a>
		    </td>
		    <td>
			<a href="#" id="newPrimeDescription" data-type="textarea"></a>
		    </td>
                    <td>
                        <a href="#" id="newPrimeISCp" data-type="text">
                             <a id='newPrimeIsEditCP' href='#' data-type='select'>                                
                            </a>
                            
                        </a>
		    </td>
		    <td>
			<button id="newPrimeAdd" class="btn btn-success">Ajouter</button>
		    </td>
		</tr>
	    </tbody>
	</table>
	<form id="newPrimeForm" action="#" method="post" action=''>
	    <input type="hidden" id="data-libelle" name="libelle" value="">	    
            <input type="hidden" id="data-valeur" name="valeur" value="">
            <input type="hidden" id="data-is_cp" name="is_cp" value="">
            <input type="hidden" id="data-description" name="description" value="">
            
	</form>
    </div>

    <!-- CONTENU AJAX -->
    <div id='content-data'>
	<!-- AJAX LOADING -->
    </div>
</div>

<script type='text/javascript'>
    
    var JsPrim = (function(){
        var isEmpty = function(selector){
                return $.trim($(selector).val()) === '';
            },
            isFloat = function(val){
                v = $.trim(val);
                patFloat = new RegExp(/^[0-9]*[.]{0,1}[0-9]+$/);

                return patFloat.test(v);                     
            }
            ; 
            
        return {
            init : function(){
                var self = this;
                
                JsPrim.refresh();
                JsPrim.initAdding();
            },
            initAdding : function(){
                 // init events adding
                $('#bt-add-prime').on('click', function(){
                    $('.add-toggle').toggle('slow');
                });
                
                //init editing
                $('#newPrimeLibelle').editable({
                title: "Libelle de la nouvelle prime",
                validate: function(value){
                    $('#data-libelle').val(value);                    
                    }
                });
                
                $('#newPrimeDescription').editable({
                title: "Description de la nouvelle prime",
                validate: function(value){
                    $('#data-description').val(value);                    
                    }
                });
                
                $('#newPrimeValeur').editable({
                title: "Valeur de la nouvelle prime",
                validate: function(value){
                    var val = $.trim(value);
                            
                    if(!isFloat(val))
                        return 'Donnée invalide.';           
                    $('#data-valeur').val(value);                    
                    }
                });
                $('#newPrimeIsEditCP').editable({
                title: "CP/ Hors CP",
                source: [{value: 1, text: "CP"}, {value: 0, text: "Hors CP"}],
                validate: function(value){
                    $('#data-is_cp').val(value);                    
                    }
                });
                
                // btn add
                $('#newPrimeAdd').on('click', function(){                    
                    if(isEmpty('#data-libelle')){
                        alert('Vous devez fournir un libellé à la nouvelle prime ');
                        return false;
                    }
                    if(isEmpty('#data-valeur')){
                        alert('Vous devez fournir une valeur à la nouvelle prime ');
                        return false;
                    }
                    if(isEmpty('#data-is_cp')){
                        alert("Vous devez fournir indiquer si c'est une prime CP ou hors CP ");
                        return false;
                    }
                    
                    ajax = $.ajax({
                        dataType: 'json',
                        type: 'post',
                        url: '<?php echo $this->url('RHX/default', array('controller'	 => 'prime', 'action'	 => 'add')) ?>',
                        data : $('#newPrimeForm').serialize()
                    });
                    ajax.success(function(res){
                        if(!res.done){
                            alert("Une erreur est survenue lors de l'insertion des données\n" + result.error.xdebug_message);
                        }
                        window.location.reload();
                    });
                });
            },
            refresh : function(){ 
                ajax = $.ajax({
                    type : "post",
                    url  : "<?php echo $this->url('RHX/default', array('controller'	 => 'prime', 'action'	 => 'loadIndex')) ?>"
                    });
                ajax.success(function(data){
                    $('#content-data').html(data);
                });
                ajax.error(function(){
                    $("#content-data").html("<div class='rh-error'>Contenu Impossible à charger</div>");
                });
            }
        } 
    } )();
    
    $(document).bind('ready', JsPrim.init);
    
</script>